import { findAllCategory } from "@/api/category"
import { topCategory } from "@/api/constants"

// 分类模块
export default {
  namespaced: true,
  state() {
    return {
      // 分类信息集合
      list: topCategory.map(item => ({ name: item }))
    }
  },
  mutations: {
    setList(state, list) {
      state.list = list
    },
    show(state, item) {
      const category = state.list.find(category => category.id === item.id)
      category.open = true
    },
    hide(state, item) {
      const category = state.list.find(category => category.id === item.id)
      category.open = false
    }
  },
  actions: {
    async getList({ commit }) {
      const { result } = await findAllCategory()
      result.forEach(element => {
        element.open = false
      });
      commit('setList', result)
    }

  }
}
